package com.ruoyi.merchant.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.merchant.domain.ImsVehViolation;
import com.ruoyi.merchant.domain.dto.ImsVehViolationDto;
import com.ruoyi.merchant.service.IImsVehViolationService;
import com.ruoyi.order.domain.dto.WxVhiceodrDto;
import com.ruoyi.order.domain.vo.WxVhiceodrVo;
import com.ruoyi.order.service.IWxVhiceodrService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.file.FileUtils.FILE_DELIMETER;


/**
 * 车辆违章信息Controller
 * 
 * @author chenyuhang
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/merchant/violation")
public class ImsVehViolationController extends BaseController
{
    @Autowired
    private IImsVehViolationService imsVehViolationService;

    @Autowired
    private IWxVhiceodrService wxVhiceodrService;

    /**
     * 查询车辆违章信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:violation:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImsVehViolation imsVehViolation)
    {
        startPage();
        List<ImsVehViolation> list = imsVehViolationService.selectImsVehViolationList(imsVehViolation);
        return getDataTable(list);
    }

    /**
     * 导出车辆违章信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:violation:export')")
    @Log(title = "车辆违章信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImsVehViolation imsVehViolation)
    {
        List<ImsVehViolation> list = imsVehViolationService.selectImsVehViolationList(imsVehViolation);
        ExcelUtil<ImsVehViolation> util = new ExcelUtil<ImsVehViolation>(ImsVehViolation.class);
        util.exportExcel(response, list, "车辆违章信息数据");
    }

    /**
     * 获取车辆违章信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:violation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imsVehViolationService.selectImsVehViolationById(id));
    }

    /**
     * 新增车辆违章信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:violation:add')")
    @Log(title = "车辆违章信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImsVehViolation imsVehViolation)
    {
        return toAjax(imsVehViolationService.insertImsVehViolation(imsVehViolation));
    }

    /**
     * 修改车辆违章信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:violation:edit')")
    @Log(title = "车辆违章信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImsVehViolation imsVehViolation)
    {
        return toAjax(imsVehViolationService.updateImsVehViolation(imsVehViolation));
    }

    /**
     * 删除车辆违章信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:violation:remove')")
    @Log(title = "车辆违章信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imsVehViolationService.deleteImsVehViolationByIds(ids));
    }

    /**
     * 车辆违章图片、 违章已处理图片上传
     */
    @Log(title = "车辆违章图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/files")
    @Anonymous
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty()) {
            for (MultipartFile file : files) {
                String vehviourl = FileUploadUtils.upload(RuoYiConfig.getVehViolationPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add("/prod-api" + vehviourl);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

    @GetMapping("/staticsVioQuantity")
    @Anonymous
    public AjaxResult staticsVioQuantity(ImsVehViolationDto imsVehViolation) {
        AjaxResult ajax = AjaxResult.success();
        // 查询未登记违章的订单
        WxVhiceodrDto imsYzzcSunOrder = new WxVhiceodrDto();
        imsYzzcSunOrder.setDeptId(imsVehViolation.getDeptId());
        imsYzzcSunOrder.setIsRecord(false);
        // 查询订单未结束且未登记违章的订单
        imsYzzcSunOrder.setRangStatus("USE_LOADING,RECEIVE_LOADING,ACCEPT_ORDERS,USE_EXPIRE,ACCEPT_RETRIEVE_ORDERS");
        List<WxVhiceodrVo> sunOrderList = wxVhiceodrService.selectWxVhiceodrList(imsYzzcSunOrder);

        // 查询未处理的违章记录
        imsVehViolation.setStatus(1);
        Integer quantity = imsVehViolationService.selectVioQuantity(imsVehViolation);
        if (ObjectUtils.isEmpty(quantity)) {
            quantity = 0;
        }
        ajax.put("quantity", quantity + sunOrderList.size());
        return ajax;
    }
}
